package com.atguigu.day03;

import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashMap;

public class Flink10_Transform_Process {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //TODO 3.使用process将数据按照空格切出每一个单词
        SingleOutputStreamOperator<String> wordDStream = streamSource.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
                String[] words = value.split(" ");
                for (String word : words) {
                    out.collect(word);
                }
            }
        });

        //TODO 4.使用process将数据组成Tuple2元组
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordToOneDStream = wordDStream.process(new ProcessFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                out.collect(Tuple2.of(value, 1));
            }
        });

        //5.将相同的单词聚合到一块
        KeyedStream<Tuple2<String, Integer>, Tuple> keyedStream = wordToOneDStream.keyBy(0);

        //TODO 6.使用process实现 sum的功能
        keyedStream.process(new KeyedProcessFunction<Tuple, Tuple2<String, Integer>, Tuple2<String, Integer>>() {

            //定义一个累加器用来保存上一次累加的结果 这里定义的变量不会区分key，所以会有bug
//            private Integer lastSum = 0;
            private HashMap<String, Integer> lastSumMap = new HashMap<>();

            @Override
            public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
//                lastSum = value.f1 + lastSum;
//                out.collect(Tuple2.of(value.f0, lastSum));


                //1.先判断key是否存在map集合中
                if (lastSumMap.containsKey(value.f0)){
                    //map中存在相同的key
                    //2.根据key获取之前计算的结果，并累加
                    Integer lastSum = lastSumMap.get(value.f0);
                    int currentSum = lastSum + value.f1;
                    //3.更新map集合中当前key计算的结果
                    lastSumMap.put(value.f0, currentSum);
                }else {
                    //map中没有当前的key的数据
                    //4.直接将当前数据存入map集合
                    lastSumMap.put(value.f0, value.f1);
                }

                out.collect(Tuple2.of(value.f0,lastSumMap.get(value.f0)));


            }
        }).print();

        env.execute();

    }
}
